Thread: error C2664: 'average' : cannot convert parameter 1 from 'int' to 'int []'

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    error C2664: 'average' : cannot convert parameter 1 from 'int' to 'int []'

    I'm currently taking the introduction to C++ class. I'm currently ahead of my class so the teacher refuses to answer my questions -_-. I've been reading through my book and looking through the web but I can't understand what my mistake is, can anyone give me hints as to what my mistake is?

    Also, I know the code is sort of a mess, I've been messing around with it alot to try and figure out the mistake so there are a few variables and instructions that aren't useful, it's just that I couldn't get my function that calculates the average to work so I just programmed the instructions into the main.

    I marked the line with the error with ==><== error here.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int carre(int x);
    double average(int a[]);
    
    int main()
    {
            int n,
                i,
                m,
                entier[5];
            double moyenne,
                   moyenne_carre,
                   sum = 0,
                   sum_carre = 0;
                   
            char check;
    
            do {
            cout << "\nVeuillez entrer 5 valeures entieres : " <<endl;
    
            for ( m = 0 ; m <= 4 ; m++ )
                cin >> entier[m];
            
    
            /*for ( m = 0 ; m <= 4 ; m++)
                sum += entier[m];
    
            moyenne = sum / 5;*/
    
            cout << "La moyenne des chiffres est :";
            for ( n = 0 ; n <= 4 ; n++ )
    ===>         cout << average(entier[n]) << " ";      <===  error here
    
            /*cout << "Les valeures au carre sont: ";
    
            for ( i = 0 ; i <= 4 ; i++ )
                cout << carre(entier[i]) << " ";
            
            
            for ( m = 0 ; m <= 4 ; m++ )
                sum_carre += carre(entier[m]);
    
            moyenne_carre = sum_carre / 5;
    
            cout << "La moyenne des chiffres au carre est:" << moyenne_carre << endl;
            */
            cout << "\nEncore? (y/n) :";
            cin >> check;
    
            } while (check == 'y');
    
    cin.sync();
    cin.get();
    }
    
    int carre(int x)
    {
        return x * x;
    }
    
    double average(int a[])
    {
        int avg = 0, sum = 0, n, i = 0;
    
        for (n = 0 ; n <= 4 ; n++)
        {    
            sum += a[n];
            i=i++;
        }
    
        avg = sum/i;
    
        return avg;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    average() is a function that accepts an array of int. When you call it, you pass a single int. A single int is not an array of int. Each element in the array is an int.

    Instead of "cout << average(entier[n])" you probably need to do "cout << average(entier)" - and not do that in a loop.

    entier is an array of four integers. entier[n] is the n'th element of that array.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Quote Originally Posted by grumpy View Post
    average() is a function that accepts an array of int. When you call it, you pass a single int. A single int is not an array of int. Each element in the array is an int.

    Instead of "cout << average(entier[n])" you probably need to do "cout << average(entier)" - and not do that in a loop.

    entier is an array of four integers. entier[n] is the n'th element of that array.
    I love you. thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: C2664 : sprintf_s() cannot convert
    By klipseracer in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2008, 02:32 AM
  2. Replies: 6
    Last Post: 12-15-2006, 07:41 AM
  3. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  4. ERROR! cannot convert parameter...
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 09-09-2003, 01:11 PM
  5. Error Cannot Convert Parameter 1
    By drdroid in forum C++ Programming
    Replies: 47
    Last Post: 08-01-2003, 09:00 PM